<template>
  <van-list
  v-model="loading"
  :finished="finished"
  :error.sync="error"
  error-text="请求失败，点击重新加载"
  finished-text="没有更多了"
  @load="onLoad"
  >
    <van-cell v-for="item in list" :key="item.art_id" :title="item.title" />
  </van-list>
</template>

<script>
import { getSearchResults } from '@/api/search.js'
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      error: false
    }
  },
  props: {
    searchTxt: {
      type: String,
      required: true
    }
  },
  created () {

  },

  methods: {
    async onLoad () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      try {
        const { data: res } = await getSearchResults({
          page: this.page,
          per_page: 10,
          q: this.searchTxt
        })
        if (Math.random() > 0.5) {
          console.lg(1)
        }
        const results = res.data.results
        this.list.push(...results)
        this.loading = false
        if (results.length) {
          this.page++
        } else {
          this.finished = true
        }
      } catch (error) {
        this.error = true
        this.loading = false
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
